package Servlet.info;

import Server.List.novelListServer;
import Server.info.userInfoServer;
import bean.novelBean;
import bean.userBean;
import dao.userDao;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

@WebServlet(name = "userInfoManagerServlet", value = "/userInfoManagerServlet")
public class userInfoManagerServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String url="userInfo.jsp";
        HttpSession session = request.getSession();
        userBean user = (userBean) session.getAttribute("user");


        try {
            if(user!=null){
                if(  user.getUserType().equals("admin") || user.getUserType().equals("user")){
                    userInfoServer server=new userInfoServer(request,response);
                    userDao dao=new userDao();

                    HashMap<String, Integer> bookSelf = dao.queryBookSelf(request);
                    List<novelBean> history = server.getHistory(bookSelf);
                    System.out.println(history);
                    session.setAttribute("history",history);


                }else
                    throw new Exception("have not bookSelf");
            }else
                throw new Exception("have not bookSelf");

        }catch (Exception e){
            System.out.println(e.getMessage());
            session.setAttribute("history", null);
        }finally {
            request.getRequestDispatcher(url).forward(request, response);		//转发
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }
}
